/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seari.bean.MenuPrivilege;
import com.seari.bean.Role;
import com.seari.bean.RolePrivilege;
import com.seari.bean.User;
import com.seari.mapper.RoleMapper;
import com.seari.mapper.RolePrivilegeMapper;
import com.seari.mapper.UserRoleMapper;
import com.seari.pojo.QueryMatch;
import com.seari.pojo.QueryRole;
import com.seari.service.RoleService;

/**
 *
 * @author Rainbow
 */
@Service("RoleService")
public class RoleServiceImpl implements  RoleService
{
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePrivilegeMapper rolePrivilegeMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    
    
    @Override
    public List<Role> getRoleByParams(QueryRole qur)
    {
        return roleMapper.getRoleByParams(qur);
    }

    @Override
    public int checkRoleExist(QueryRole qur)
    {
        return roleMapper.checkRoleExist(qur);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRole(Role role,RolePrivilege rolePrivilege)
    {
         roleMapper.addRole(role);
         rolePrivilege.setUrid(role.getUrid());
         // 添加角色时获取该角色ID , 删除中间表相应ID的权限
         rolePrivilegeMapper.deleteRolePrivilegeByUurid(role.getUrid());
         // 为角色添加权限
         return rolePrivilegeMapper.addRolePrivilege(rolePrivilege);
        
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRole(Role role,RolePrivilege rolePrivilege)
    {
         roleMapper.updateRole(role);
         // 添加角色时获取该角色ID , 删除中间表相应ID的权限
         rolePrivilegeMapper.deleteRolePrivilegeByUurid(role.getUrid());
         // 为角色添加权限
         return rolePrivilegeMapper.addRolePrivilege(rolePrivilege);
         
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRole(int urid)
    {
    	rolePrivilegeMapper.deleteRolePrivilegeByUurid(urid);
    	userRoleMapper.deleteUserRoleByUrid(urid);
        return roleMapper.deleteRole(urid);
    }

	@Override
	public int getRoleCount()
	{
		return roleMapper.getRoleCount();
	}

	@Override
	public List<Role> getRoleByUrid(QueryRole qur) {
		// TODO Auto-generated method stub
		 List<Role> roleByUrid = roleMapper.getRoleByUrid(qur);
		 for (int i = 0; i < roleByUrid.size(); i++) {
			 List<MenuPrivilege> menuPrivileges = roleByUrid.get(i).getMenuPrivileges();
			 for (int j = 0; j < menuPrivileges.size(); j++) {
				 int parentId = menuPrivileges.get(j).getParentId();
				if (parentId != 0) {
					String parentName = roleMapper.getPmNameByParentId(parentId);
					menuPrivileges.get(j).setParentName(parentName);
				}
			}
		}
		 return roleByUrid;
	}

	@Override
	public List<Role> roleSearch(QueryRole qu) {
		// TODO Auto-generated method stub
		return roleMapper.roleSearch(qu);
	}

	@Override
	public List<User> getUserRoleByUrid(int urid) {
		// TODO Auto-generated method stub
		return roleMapper.getUserRoleByUrid(urid);
	}

	@Override
	public List<MenuPrivilege> getMenuByUrids(List<Integer> roleIds) {
		// TODO Auto-generated method stub
		 List<MenuPrivilege> menuByUrids = roleMapper.getMenuByUrids(roleIds);
		 for (int j = 0; j < menuByUrids.size(); j++) {
			 int parentId = menuByUrids.get(j).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				menuByUrids.get(j).setParentName(parentName);
			}
		}
		 return menuByUrids;
	}
    
}
